package com.lin.linchatgpt.openai.server.imple;

import com.lin.linchatgpt.entity.Message;
import com.lin.linchatgpt.entity.dto.MessageDTO;
import com.lin.linchatgpt.entity.table.MessageTableDef;
import com.lin.linchatgpt.openai.server.HistoryHandlerMessageStrategy;
import com.lin.linchatgpt.service.MessageService;
import com.lin.linchatgpt.utils.NanoMillisUtils;
import com.mybatisflex.core.query.QueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;


import java.util.List;

/**
 * Copyright(C),2022年-2022,霖霖
 *
 * @author 霖霖
 * @version 1.0
 * @date 2023/6/22 20:15
 * @Description 用于既不是新建主题消息，也不是修改消失
 */

@Service
public class OrdinaryMessage implements HistoryHandlerMessageStrategy {
    private final MessageService messageService;
    public OrdinaryMessage(MessageService messageService) {
        this.messageService = messageService;
    }

    @Override
    public List<Message> historyMessageHandler( MessageDTO message) {

        QueryWrapper queryWrapper = new QueryWrapper();
        // 有父级消息时，查询所有父级记录
        queryWrapper.where(MessageTableDef.MESSAGE.SUBJECT_ID.eq(message.getSubjectId()));
        queryWrapper.orderBy(MessageTableDef.MESSAGE.RECORD_TIME.asc());
        List<Message> history = messageService.list(queryWrapper);

        Message targetMessage = new Message();
        BeanUtils.copyProperties(message, targetMessage);
        targetMessage.setRecordTime(NanoMillisUtils.getMillisTime());


        history.add(targetMessage);

        return history;
    }
}
